﻿using System;
using System.Text.RegularExpressions;

namespace LdtJudge.DataEngine.Common.DateAssemble
{
    /// <summary>
    /// 时间处理类
    /// </summary>
    public class DateTimeFormat
    {
        /// <summary>
        /// 调用转时间的方法入口
        /// </summary>
        /// <param name="TypeCallProgram"></param>
        /// <param name="Time"></param>
        /// <returns></returns>
        public static string ConvertEntrance(string TypeCallProgram, string Time)
        {
            switch (TypeCallProgram.ToLower().Trim())
            {
                case "converttoyear":
                    return ConvertToYear(Time);
                case "converttomonth":
                    return ConvertToMonth(Time);
                case "converttoday":
                    return ConvertToDay(Time);
                case "converttodate":
                    DateTime parseDate;
                    bool r = DateTime.TryParse(Time, out parseDate);
                    if (r)
                    {
                        return parseDate.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        return "";
                    }
                case "converttostandardamount":
                    //进行金额转换
                    AmountConvert amountConvert = new AmountConvert();
                    return amountConvert.DataConvertNumber(Time, false);
                    
                default:
                    return Time;
            }


        }

        /// <summary>
        /// 将时间进行转换为标准年的格式
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string ConvertToYear(string time)
        {
            int year = CutYear(time);
            int month = CutMonth(time);
            int day = CutDay(time);
            double totalyear = Convert.ToDouble(month * 30 + day) / 365 + year;
            return totalyear.ToString("0.00");
        }

        /// <summary>
        /// 将具体的时间转换为标准月
        /// 1年2个月 =14
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string ConvertToMonth(string time)
        {
            int year = CutYear(time);
            int month = CutMonth(time);
            int day = CutDay(time);
            double totalmonth = Convert.ToDouble(day / 30) + year * 12 + month;
            return totalmonth.ToString("0.00");
        }

        /// <summary>
        /// 将具体的时间转换为标准的天
        /// 例如：2个月=60；
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string ConvertToDay(string time)
        {
            int year = CutYear(time);
            int month = CutMonth(time);
            int day = CutDay(time);
            int totalday = year * 365 + month * 30 + day;
            return totalday.ToString();
        }

        /// <summary>
        /// 进行截取日期中的年
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private static int CutYear(string time)
        {
            Match ma = Regex.Match(time, @"[一二三四五六七八九十\d]+(?=年)");
            if (ma.Success)
            {
                string year = FormatNumber(ma.Value);
                int res = 0;
                if (Int32.TryParse(year, out res))
                {
                    //如果整数转换成功，则进行输出返回;
                    return res;
                }
            }
            return 0;
        }

        /// <summary>
        /// 将时间月获取到
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private static int CutMonth(string time)
        {
            Match ma = Regex.Match(time, @"[一二三四五六七八九十\d]+(?=个?月)");
            if (ma.Success)
            {
                string year = FormatNumber(ma.Value);
                int res = 0;
                if (Int32.TryParse(year, out res))
                {
                    //如果整数转换成功，则进行输出返回;
                    return res;
                }
            }
            return 0;
        }

        /// <summary>
        /// 获取到具体的天
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private static int CutDay(string time)
        {
            Match ma = Regex.Match(time, @"[一二三四五六七八九十\d]+(?=天)");
            if (ma.Success)
            {
                string year = FormatNumber(ma.Value);
                int res = 0;
                if (Int32.TryParse(year, out res))
                {
                    //如果整数转换成功，则进行输出返回;
                    return res;
                }
            }
            return 0;
        }

        /// <summary>
        /// 进行时间转换
        /// </summary>
        /// <param name="currValue"></param>
        /// <returns></returns>
        private static string FormatNumber(string currValue)
        {
            double resultValue = 0;
            currValue = currValue.Replace("〇", "零");
            if (Regex.IsMatch(currValue, "^十"))
            {
                currValue = Regex.Replace(currValue, "^十", "一十");

                if (currValue.Substring(currValue.Length - 1) == "十")
                {
                    currValue = Regex.Replace(currValue, "一", "");
                }
                else
                {
                    currValue = Regex.Replace(currValue, "十", "");
                }
            }
            else
            {
                //修改日期转换问题。
                currValue = Regex.Replace(currValue, "十$", " ");
                if (Regex.IsMatch(currValue, "^(?!十).*?(?=十).*(?<!十)$"))
                    currValue = Regex.Replace(currValue, "十", "");
            }

            for (int i = 0; i < currValue.Length; i++)
            {
                char vChar = currValue[i];

                int j = 0;
                if (Regex.IsMatch(vChar.ToString(), @"\d"))
                {
                    j = int.Parse(vChar.ToString());
                }

                else
                {
                    j = "零一二三四五六七八九十".IndexOf(vChar);
                }
                if (j >= 0)
                {
                    resultValue += j * Math.Pow(10, currValue.Length - i - 1);
                }
            }

            return resultValue.ToString();
        }

        /// <summary>
        /// 格式化中文日期
        /// </summary>
        /// <param name="currValue">字段值</param>
        /// <returns>格式化的值</returns>
        public static string FormatChDateTimeNumber(string currValue)
        {
            //double resultValue = 0;

            //currValue = currValue.Replace("〇", "零");
            //if (Regex.IsMatch(currValue, "^十"))
            //{
            //    //currValue = Regex.Replace(currValue, "^十", "一十");

            //    //if (currValue.Substring(currValue.Length - 1) != "十")
            //    //{
            //    //    currValue = Regex.Replace(currValue, "十", "");
            //    //}
            //}
            //else
            //{
            //    //currValue = Regex.Replace(currValue, "十", "");
            //}



            //for (int i = 0; i < currValue.Length; i++)
            //{
            //    char vChar = currValue[i];

            //    int j = 0;
            //    if (Regex.IsMatch(vChar.ToString(), @"\d"))
            //    {
            //        j = int.Parse(vChar.ToString());
            //    }

            //    else
            //    {
            //        j = "零一二三四五六七八九十".IndexOf(vChar);
            //    }
            //    if (j >= 0)
            //    {
            //        resultValue += j * Math.Pow(10, currValue.Length - i - 1);
            //    }
            //}
            //if (currValue=="十")
            //{
            //    resultValue = 10;
            //}
            //if (currValue == "二十")
            //{
            //    resultValue = 20;
            //}
            //if (currValue == "三十")
            //{
            //    resultValue = 30;
            //}
            //if (currValue == "四十")
            //{
            //    resultValue = 40;
            //}

            double resultValue = 0;
            currValue = currValue.Replace("〇", "零");
            if (Regex.IsMatch(currValue, "^十"))
            {
                currValue = Regex.Replace(currValue, "^十", "一十");

                if (currValue.Substring(currValue.Length - 1) == "十")
                {
                    currValue = Regex.Replace(currValue, "一", "");
                }
                else
                {
                    currValue = Regex.Replace(currValue, "十", "");
                }
            }
            else
            {
                //修改日期转换问题。
                currValue = Regex.Replace(currValue, "十$", " ");
                if (Regex.IsMatch(currValue, "^(?!十).*?(?=十).*(?<!十)$"))
                    currValue = Regex.Replace(currValue, "十", "");
            }

            for (int i = 0; i < currValue.Length; i++)
            {
                char vChar = currValue[i];

                int j = 0;
                if (Regex.IsMatch(vChar.ToString(), @"\d"))
                {
                    j = int.Parse(vChar.ToString());
                }

                else
                {
                    j = "零一二三四五六七八九十".IndexOf(vChar);
                }
                if (j >= 0)
                {
                    resultValue += j * Math.Pow(10, currValue.Length - i - 1);
                }
            }

            return resultValue.ToString();
            //return resultValue.ToString();
        }


        /// <summary>
        /// Converts to date.
        /// </summary>
        /// <param name="convertValue">The convert value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertToDate(string convertValue)
        {
            try
            {

                DateTime parseDate;
                bool r = DateTime.TryParse(convertValue, out parseDate);

                if (!r)
                {
                    Regex reg = new Regex("[0-9.]+");
                    string result = reg.Match(convertValue).Value;
                    //2018-01-02 添加日期转换，验证，如果是不是日期格式，则进行转换
                    if (!string.IsNullOrEmpty(result))
                        parseDate = DateTime.Parse(result);

                }
                else
                {
                    parseDate = Convert.ToDateTime(convertValue);
                }

                int year = parseDate.Year;
                int month = parseDate.Month;
                int date = parseDate.Day;

                Regex regx = new Regex(@"\d");
                
                MatchCollection mact = regx.Matches(convertValue);
                string datlet = string.Empty;
                foreach (Match item in mact)
                {
                    datlet += item.Value;
                }
                string NewDateTime = string.Empty;
                string datestr = datlet;
                if (!string.IsNullOrEmpty(datestr) && year == Convert.ToInt32(datestr.Substring(0, year.ToString().Length)))
                {
                    datestr = datestr.Substring(year.ToString().Length);
                    NewDateTime += year + "年";
                    convertValue = NewDateTime;
                }
                datestr = datestr.TrimStart('0');
                if (!string.IsNullOrEmpty(datestr) && month == Convert.ToInt32(datestr.Substring(0, month.ToString().Length)))
                {
                    datestr = datestr.Substring(month.ToString().Length);
                    NewDateTime += month.ToString().PadLeft(2, '0') + "月";
                    convertValue = NewDateTime;
                }
                datestr = datestr.TrimStart('0');
                if (!string.IsNullOrEmpty(datestr) && date == Convert.ToInt32(datestr.Substring(0, date.ToString().Length)))
                {
                    datestr = datestr.Substring(date.ToString().Length);
                    NewDateTime += date.ToString().PadLeft(2, '0') + "日";
                    convertValue = NewDateTime;
                }

            }
            catch (Exception e)
            {

                if (Regex.IsMatch(convertValue, @"[〇零一二三四五六七八九十\s]+"))
                {
                    string year = Regex.Match(convertValue, "[^年]+(?=年)").Value;
                    string month = Regex.Match(convertValue, "[^年月]+(?=月)").Value;
                    string day = Regex.Match(convertValue, "[^年月日]+(?=日)").Value;

                    year = DateTimeFormat.FormatChDateTimeNumber(year).ToString();
                    month = DateTimeFormat.FormatChDateTimeNumber(month).ToString().PadLeft(2,'0');
                    day = DateTimeFormat.FormatChDateTimeNumber(day).ToString().PadLeft(2,'0');
                    day = (day == "00") ? "01" : day;

                    convertValue = year + "年" + month + "月" + day+"日";
                }
            }

            return convertValue;
        }

    }
}
